<template>
<!-- excel表格显示 -->
  <div id="table">
    <el-table :data="tableData" style="width: 100%">
      <el-table-column
        v-for="(value,key,index) in tableData"
        :key="index"
        :prop="key"
        :label="key"
      ></el-table-column>
    </el-table>
  </div>
</template>

<script>
import XLSX from 'xlsx'
export default {
  data () {
    return {
      tableData: [],
      workbook: {},
      excelURL: 'https://oss.databstech.com/upload/20220906160707675.xlsx'
      // 'https://oss.databstech.com/upload/20220907113655540.xls'
    }
  },
  mounted () {
    // this.excelURL = this.$route.query.excelURL
    this.readWorkbookFromRemoteFile(this.excelURL)
  },
  methods: {
    readWorkbookFromRemoteFile: function (url) {
      var xhr = new XMLHttpRequest()
      console.log('打印', url)

      xhr.open('get', url, true)
      xhr.responseType = 'arraybuffer'
      const _this = this
      xhr.onload = function (e) {
        if (xhr.status === 200) {
          var data = new Uint8Array(xhr.response)
          var workbook = XLSX.read(data, { type: 'array' })
          console.log(workbook)

          var sheetNames = workbook.SheetNames // 工作表名称集合
          _this.workbook = workbook
          _this.getTable(sheetNames[0])
        }
      }
      xhr.send()
    },
    getTable (sheetName) {
      console.log(sheetName)
      var worksheet = this.workbook.Sheets[sheetName]
      this.tableData = XLSX.utils.sheet_to_json(worksheet)
      console.log(this.tableData)
    }
  }
}
</script>

<style lang="scss" scoped>
#table {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
  margin-top: 60px;
  border: 1px solid #ebebeb;
  padding: 20px;
  width: 80%;
  margin: 20px auto;
  // border-shadow: 0 0 8px 0 rgba(232, 237, 250, 0.6), 0 2px 4px 0 rgba(232, 237, 250, 0.5);
  border-radius: 10px;
  overflow: scroll;
  height: 100%;

  .tab {
    margin: 0 0 20px 0;
    display: flex;
    flex-direction: row;
  }
}
</style>

<style scoped>
.is-active {
  background-color: #409eff;
}
span {
  background-color: red;
}
</style>
